<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Ajax设置请求头</title>
    <style>
        #result {
            width: 200px;
            height: 100px;
            border: solid 1px #90b;
        }
    </style>
</head>
<body>
<button>点击发送请求</button>
<div id="result"></div>
<script>

    const btn = document.querySelector('button')
    btn.addEventListener("click", function () {
        const xhr = new XMLHttpRequest()
        xhr.open('POST', 'http://localhost:8000/server')
        //设置请求头
        //设置请求体查询   参数的类型
        xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded')
        xhr.setRequestHeader('name', 'zyj')  //自定义请求头
        xhr.send('a=200&b=300')

        xhr.onreadystatechange = function () {
            if (xhr.readyState === 4) {
                if (xhr.status >= 200 && xhr.status < 300) {
                    console.log(xhr.response);//响应体
                    document.querySelector('div').innerHTML = `${xhr.response}`
                }
            }

        }
    })
</script>
</body>
</html>